use Hooks;
use IBufferingStatsdDataFactory;
use MediaWiki\Shell\CommandFactory;
+use MediaWiki\Storage\BlobStore;
+use MediaWiki\Storage\RevisionStore;
use Wikimedia\Rdbms\LBFactory;
use LinkCache;
use Wikimedia\Rdbms\LoadBalancer;
return $this->getService( 'ExternalStoreFactory' );
}
+ /**
+ * @since 1.31
+ * @return BlobStore
+ */
+ public function getBlobStore() {
+ return $this->getService( 'BlobStore' );
+ }
+
+ /**
+ * @since 1.31
+ * @return RevisionStore
+ */
+ public function getRevisionStore() {
+ return $this->getService( 'RevisionStore' );
+ }
+
///////////////////////////////////////////////////////////////////////////
// NOTE: When adding a service getter here, don't forget to add a test
// case for it in MediaWikiServicesTest::provideGetters() and in
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MediaWikiServices;
use MediaWiki\Shell\CommandFactory;
+use MediaWiki\Storage\RevisionStore;
+use MediaWiki\Storage\SqlBlobStore;
return [
'DBLoadBalancerFactory' => function ( MediaWikiServices $services ) {
);
},
+ 'RevisionStore' => function ( MediaWikiServices $services ) {
+ /** @var SqlBlobStore $blobStore */
+ $blobStore = $services->getService( '_SqlBlobStore' );
+
+ $store = new RevisionStore(
+ $services->getDBLoadBalancer(),
+ $blobStore,
+ $services->getMainWANObjectCache()
+ );
+
+ $config = $services->getMainConfig();
+ $store->setContentHandlerUseDB( $config->get( 'ContentHandlerUseDB' ) );
+
+ return $store;
+ },
+
+ 'BlobStore' => function ( MediaWikiServices $services ) {
+ return $services->getService( '_SqlBlobStore' );
+ },
+
+ '_SqlBlobStore' => function ( MediaWikiServices $services ) {
+ global $wgContLang; // TODO: manage $wgContLang as a service
+
+ $store = new SqlBlobStore(
+ $services->getDBLoadBalancer(),
+ $services->getMainWANObjectCache()
+ );
+
+ $config = $services->getMainConfig();
+ $store->setCompressBlobs( $config->get( 'CompressRevisions' ) );
+ $store->setCacheExpiry( $config->get( 'RevisionCacheExpiry' ) );
+ $store->setUseExternalStore( $config->get( 'DefaultExternalStore' ) !== false );
+
+ if ( $config->get( 'LegacyEncoding' ) ) {
+ $store->setLegacyEncoding( $config->get( 'LegacyEncoding' ), $wgContLang );
+ }
+
+ return $store;
+ },
+
///////////////////////////////////////////////////////////////////////////
// NOTE: When adding a service here, don't forget to add a getter function
// in the MediaWikiServices class. The convenience getter should just call
--- /dev/null
+<?php
+
+namespace MediaWiki\Tests\Storage;
+
+use CommentStoreComment;
+use MediaWiki\Storage\MutableRevisionRecord;
+use MediaWiki\Storage\RevisionAccessException;
+use MediaWiki\Storage\RevisionRecord;
+use MediaWiki\Storage\SlotRecord;
+use MediaWikiTestCase;
+use Title;
+use WikitextContent;
+
+/**
+ * @covers \MediaWiki\Storage\MutableRevisionRecord
+ */
+class MutableRevisionRecordTest extends MediaWikiTestCase {
+
+ public function testSimpleSetGetId() {
+ $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
+ $this->assertNull( $record->getId() );
+ $record->setId( 888 );
+ $this->assertSame( 888, $record->getId() );
+ }
+
+ public function testSimpleSetGetUser() {
+ $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
+ $user = $this->getTestSysop()->getUser();
+ $this->assertNull( $record->getUser() );
+ $record->setUser( $user );
+ $this->assertSame( $user, $record->getUser() );
+ }
+
+ public function testSimpleSetGetPageId() {
+ $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
+ $this->assertSame( 0, $record->getPageId() );
+ $record->setPageId( 999 );
+ $this->assertSame( 999, $record->getPageId() );
+ }
+
+ public function testSimpleSetGetParentId() {
+ $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
+ $this->assertNull( $record->getParentId() );
+ $record->setParentId( 100 );
+ $this->assertSame( 100, $record->getParentId() );
+ }
+
+ public function testSimpleGetMainContentWhenEmpty() {
+ $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
+ $this->setExpectedException( RevisionAccessException::class );
+ $this->assertNull( $record->getContent( 'main' ) );
+ }
+
+ public function testSimpleSetGetMainContent() {
+ $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
+ $content = new WikitextContent( 'Badger' );
+ $record->setContent( 'main', $content );
+ $this->assertSame( $content, $record->getContent( 'main' ) );
+ }
+
+ public function testSimpleGetSlotWhenEmpty() {
+ $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
+ $this->setExpectedException( RevisionAccessException::class );
+ $record->getSlot( 'main' );
+ }
+
+ public function testSimpleSetGetSlot() {
+ $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
+ $slot = new SlotRecord(
+ (object)[ 'role_name' => 'main' ],
+ new WikitextContent( 'x' )
+ );
+ $record->setSlot( $slot );
+ $this->assertSame( $slot, $record->getSlot( 'main' ) );
+ }
+
+ public function testSimpleSetGetMinor() {
+ $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
+ $this->assertFalse( $record->isMinor() );
+ $record->setMinorEdit( true );
+ $this->assertSame( true, $record->isMinor() );
+ }
+
+ public function testSimpleSetGetTimestamp() {
+ $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
+ $this->assertNull( $record->getTimestamp() );
+ $record->setTimestamp( '20180101010101' );
+ $this->assertSame( '20180101010101', $record->getTimestamp() );
+ }
+
+ public function testSimpleSetGetVisibility() {
+ $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
+ $this->assertSame( 0, $record->getVisibility() );
+ $record->setVisibility( RevisionRecord::DELETED_USER );
+ $this->assertSame( RevisionRecord::DELETED_USER, $record->getVisibility() );
+ }
+
+ public function testSimpleSetGetSha1() {
+ $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
+ $this->assertSame( 'phoiac9h4m842xq45sp7s6u21eteeq1', $record->getSha1() );
+ $record->setSha1( 'someHash' );
+ $this->assertSame( 'someHash', $record->getSha1() );
+ }
+
+ public function testSimpleSetGetSize() {
+ $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
+ $this->assertSame( 0, $record->getSize() );
+ $record->setSize( 775 );
+ $this->assertSame( 775, $record->getSize() );
+ }
+
+ public function testSimpleSetGetComment() {
+ $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
+ $comment = new CommentStoreComment( 1, 'foo' );
+ $this->assertNull( $record->getComment() );
+ $record->setComment( $comment );
+ $this->assertSame( $comment, $record->getComment() );
+ }
+
+}
--- /dev/null
+<?php
+
+namespace MediaWiki\Tests\Storage;
+
+use MediaWiki\Storage\MutableRevisionSlots;
+use MediaWiki\Storage\RevisionAccessException;
+use MediaWiki\Storage\SlotRecord;
+use MediaWikiTestCase;
+use WikitextContent;
+
+/**
+ * @covers \MediaWiki\Storage\MutableRevisionSlots
+ */
+class MutableRevisionSlotsTest extends MediaWikiTestCase {
+
+ public function testSetMultipleSlots() {
+ $slots = new MutableRevisionSlots();
+
+ $this->assertSame( [], $slots->getSlots() );
+
+ $slotA = SlotRecord::newUnsaved( 'some', new WikitextContent( 'A' ) );
+ $slots->setSlot( $slotA );
+ $this->assertSame( $slotA, $slots->getSlot( 'some' ) );
+ $this->assertSame( [ 'some' => $slotA ], $slots->getSlots() );
+
+ $slotB = SlotRecord::newUnsaved( 'other', new WikitextContent( 'B' ) );
+ $slots->setSlot( $slotB );
+ $this->assertSame( $slotB, $slots->getSlot( 'other' ) );
+ $this->assertSame( [ 'some' => $slotA, 'other' => $slotB ], $slots->getSlots() );
+ }
+
+ public function testSetExistingSlotOverwritesSlot() {
+ $slots = new MutableRevisionSlots();
+
+ $this->assertSame( [], $slots->getSlots() );
+
+ $slotA = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) );
+ $slots->setSlot( $slotA );
+ $this->assertSame( $slotA, $slots->getSlot( 'main' ) );
+ $this->assertSame( [ 'main' => $slotA ], $slots->getSlots() );
+
+ $slotB = SlotRecord::newUnsaved( 'main', new WikitextContent( 'B' ) );
+ $slots->setSlot( $slotB );
+ $this->assertSame( $slotB, $slots->getSlot( 'main' ) );
+ $this->assertSame( [ 'main' => $slotB ], $slots->getSlots() );
+ }
+
+ public function testSetContentOfExistingSlotOverwritesContent() {
+ $slots = new MutableRevisionSlots();
+
+ $this->assertSame( [], $slots->getSlots() );
+
+ $slotA = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) );
+ $slots->setSlot( $slotA );
+ $this->assertSame( $slotA, $slots->getSlot( 'main' ) );
+ $this->assertSame( [ 'main' => $slotA ], $slots->getSlots() );
+
+ $newContent = new WikitextContent( 'B' );
+ $slots->setContent( 'main', $newContent );
+ $this->assertSame( $newContent, $slots->getContent( 'main' ) );
+ }
+
+ public function testRemoveExistingSlot() {
+ $slotA = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) );
+ $slots = new MutableRevisionSlots( [ $slotA ] );
+
+ $this->assertSame( [ 'main' => $slotA ], $slots->getSlots() );
+
+ $slots->removeSlot( 'main' );
+ $this->assertSame( [], $slots->getSlots() );
+ $this->setExpectedException( RevisionAccessException::class );
+ $slots->getSlot( 'main' );
+ }
+
+}
use MediaWikiTestCase;
/**
- * @covers MediaWiki\Storage\RevisionRecord
+ * @covers \MediaWiki\Storage\RevisionRecord
*/
class RevisionRecordTest extends MediaWikiTestCase {
--- /dev/null
+<?php
+
+namespace MediaWiki\Tests\Storage;
+
+use MediaWiki\Storage\RevisionAccessException;
+use MediaWiki\Storage\RevisionSlots;
+use MediaWiki\Storage\SlotRecord;
+use MediaWikiTestCase;
+use WikitextContent;
+
+class RevisionSlotsTest extends MediaWikiTestCase {
+
+ /**
+ * @covers RevisionSlots::getSlot
+ */
+ public function testGetSlot() {
+ $mainSlot = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) );
+ $auxSlot = SlotRecord::newUnsaved( 'aux', new WikitextContent( 'B' ) );
+ $slots = new RevisionSlots( [ $mainSlot, $auxSlot ] );
+
+ $this->assertSame( $mainSlot, $slots->getSlot( 'main' ) );
+ $this->assertSame( $auxSlot, $slots->getSlot( 'aux' ) );
+ $this->setExpectedException( RevisionAccessException::class );
+ $slots->getSlot( 'nothere' );
+ }
+
+ /**
+ * @covers RevisionSlots::getContent
+ */
+ public function testGetContent() {
+ $mainContent = new WikitextContent( 'A' );
+ $auxContent = new WikitextContent( 'B' );
+ $mainSlot = SlotRecord::newUnsaved( 'main', $mainContent );
+ $auxSlot = SlotRecord::newUnsaved( 'aux', $auxContent );
+ $slots = new RevisionSlots( [ $mainSlot, $auxSlot ] );
+
+ $this->assertSame( $mainContent, $slots->getContent( 'main' ) );
+ $this->assertSame( $auxContent, $slots->getContent( 'aux' ) );
+ $this->setExpectedException( RevisionAccessException::class );
+ $slots->getContent( 'nothere' );
+ }
+
+ /**
+ * @covers RevisionSlots::getSlotRoles
+ */
+ public function testGetSlotRoles_someSlots() {
+ $mainSlot = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) );
+ $auxSlot = SlotRecord::newUnsaved( 'aux', new WikitextContent( 'B' ) );
+ $slots = new RevisionSlots( [ $mainSlot, $auxSlot ] );
+
+ $this->assertSame( [ 'main', 'aux' ], $slots->getSlotRoles() );
+ }
+
+ /**
+ * @covers RevisionSlots::getSlotRoles
+ */
+ public function testGetSlotRoles_noSlots() {
+ $slots = new RevisionSlots( [] );
+
+ $this->assertSame( [], $slots->getSlotRoles() );
+ }
+
+ /**
+ * @covers RevisionSlots::getSlots
+ */
+ public function testGetSlots() {
+ $mainSlot = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) );
+ $auxSlot = SlotRecord::newUnsaved( 'aux', new WikitextContent( 'B' ) );
+ $slotsArray = [ $mainSlot, $auxSlot ];
+ $slots = new RevisionSlots( $slotsArray );
+
+ $this->assertEquals( [ 'main' => $mainSlot, 'aux' => $auxSlot ], $slots->getSlots() );
+ }
+
+ public function provideComputeSize() {
+ yield [ 1, [ 'A' ] ];
+ yield [ 2, [ 'AA' ] ];
+ yield [ 4, [ 'AA', 'X', 'H' ] ];
+ }
+
+ /**
+ * @dataProvider provideComputeSize
+ * @covers RevisionSlots::computeSize
+ */
+ public function testComputeSize( $expected, $contentStrings ) {
+ $slotsArray = [];
+ foreach ( $contentStrings as $key => $contentString ) {
+ $slotsArray[] = SlotRecord::newUnsaved( strval( $key ), new WikitextContent( $contentString ) );
+ }
+ $slots = new RevisionSlots( $slotsArray );
+
+ $this->assertSame( $expected, $slots->computeSize() );
+ }
+
+ public function provideComputeSha1() {
+ yield [ 'ctqm7794fr2dp1taki8a88ovwnvmnmj', [ 'A' ] ];
+ yield [ 'eyq8wiwlcofnaiy4eid97gyfy60uw51', [ 'AA' ] ];
+ yield [ 'lavctqfpxartyjr31f853drgfl4kj1g', [ 'AA', 'X', 'H' ] ];
+ }
+
+ /**
+ * @dataProvider provideComputeSha1
+ * @covers RevisionSlots::computeSha1
+ * @note this test is a bit brittle as the hashes are hardcoded, perhaps just check that strings
+ * are returned and different Slots objects return different strings?
+ */
+ public function testComputeSha1( $expected, $contentStrings ) {
+ $slotsArray = [];
+ foreach ( $contentStrings as $key => $contentString ) {
+ $slotsArray[] = SlotRecord::newUnsaved( strval( $key ), new WikitextContent( $contentString ) );
+ }
+ $slots = new RevisionSlots( $slotsArray );
+
+ $this->assertSame( $expected, $slots->computeSha1() );
+ }
+
+}
--- /dev/null
+<?php
+
+namespace MediaWiki\Tests\Storage;
+
+use CommentStoreComment;
+use Exception;
+use InvalidArgumentException;
+use MediaWiki\Linker\LinkTarget;
+use MediaWiki\MediaWikiServices;
+use MediaWiki\Storage\IncompleteRevisionException;
+use MediaWiki\Storage\MutableRevisionRecord;
+use MediaWiki\Storage\RevisionRecord;
+use MediaWiki\Storage\SlotRecord;
+use MediaWikiTestCase;
+use Revision;
+use TestUserRegistry;
+use Title;
+use WikiPage;
+use WikitextContent;
+
+/**
+ * @group Database
+ */
+class RevisionStoreDbTest extends MediaWikiTestCase {
+
+ private function assertLinkTargetsEqual( LinkTarget $l1, LinkTarget $l2 ) {
+ $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
+ $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
+ $this->assertEquals( $l1->getFragment(), $l2->getFragment() );
+ $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() );
+ }
+
+ private function assertRevisionRecordsEqual( RevisionRecord $r1, RevisionRecord $r2 ) {
+ $this->assertEquals( $r1->getUser()->getName(), $r2->getUser()->getName() );
+ $this->assertEquals( $r1->getUser()->getId(), $r2->getUser()->getId() );
+ $this->assertEquals( $r1->getComment(), $r2->getComment() );
+ $this->assertEquals( $r1->getPageAsLinkTarget(), $r2->getPageAsLinkTarget() );
+ $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() );
+ $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() );
+ $this->assertEquals( $r1->getSha1(), $r2->getSha1() );
+ $this->assertEquals( $r1->getParentId(), $r2->getParentId() );
+ $this->assertEquals( $r1->getSize(), $r2->getSize() );
+ $this->assertEquals( $r1->getPageId(), $r2->getPageId() );
+ $this->assertEquals( $r1->getSlotRoles(), $r2->getSlotRoles() );
+ $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() );
+ $this->assertEquals( $r1->isMinor(), $r2->isMinor() );
+ foreach ( $r1->getSlotRoles() as $role ) {
+ $this->assertEquals( $r1->getSlot( $role ), $r2->getSlot( $role ) );
+ $this->assertEquals( $r1->getContent( $role ), $r2->getContent( $role ) );
+ }
+ foreach ( [
+ RevisionRecord::DELETED_TEXT,
+ RevisionRecord::DELETED_COMMENT,
+ RevisionRecord::DELETED_USER,
+ RevisionRecord::DELETED_RESTRICTED,
+ ] as $field ) {
+ $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) );
+ }
+ }
+
+ /**
+ * @param mixed[] $details
+ *
+ * @return RevisionRecord
+ */
+ private function getRevisionRecordFromDetailsArray( $title, $details = [] ) {
+ // Convert some values that can't be provided by dataProviders
+ $page = WikiPage::factory( $title );
+ if ( isset( $details['user'] ) && $details['user'] === true ) {
+ $details['user'] = $this->getTestUser()->getUser();
+ }
+ if ( isset( $details['page'] ) && $details['page'] === true ) {
+ $details['page'] = $page->getId();
+ }
+ if ( isset( $details['parent'] ) && $details['parent'] === true ) {
+ $details['parent'] = $page->getLatest();
+ }
+
+ // Create the RevisionRecord with any available data
+ $rev = new MutableRevisionRecord( $title );
+ isset( $details['slot'] ) ? $rev->setSlot( $details['slot'] ) : null;
+ isset( $details['parent'] ) ? $rev->setParentId( $details['parent'] ) : null;
+ isset( $details['page'] ) ? $rev->setPageId( $details['page'] ) : null;
+ isset( $details['size'] ) ? $rev->setSize( $details['size'] ) : null;
+ isset( $details['sha1'] ) ? $rev->setSha1( $details['sha1'] ) : null;
+ isset( $details['comment'] ) ? $rev->setComment( $details['comment'] ) : null;
+ isset( $details['timestamp'] ) ? $rev->setTimestamp( $details['timestamp'] ) : null;
+ isset( $details['minor'] ) ? $rev->setMinorEdit( $details['minor'] ) : null;
+ isset( $details['user'] ) ? $rev->setUser( $details['user'] ) : null;
+ isset( $details['visibility'] ) ? $rev->setVisibility( $details['visibility'] ) : null;
+ isset( $details['id'] ) ? $rev->setId( $details['id'] ) : null;
+
+ return $rev;
+ }
+
+ private function getRandomCommentStoreComment() {
+ return CommentStoreComment::newUnsavedComment( __METHOD__ . '.' . rand( 0, 1000 ) );
+ }
+
+ public function provideInsertRevisionOn_successes() {
+ yield 'Bare minimum revision insertion' => [
+ Title::newFromText( 'UTPage' ),
+ [
+ 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
+ 'parent' => true,
+ 'comment' => $this->getRandomCommentStoreComment(),
+ 'timestamp' => '20171117010101',
+ 'user' => true,
+ ],
+ ];
+ yield 'Detailed revision insertion' => [
+ Title::newFromText( 'UTPage' ),
+ [
+ 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
+ 'parent' => true,
+ 'page' => true,
+ 'comment' => $this->getRandomCommentStoreComment(),
+ 'timestamp' => '20171117010101',
+ 'user' => true,
+ 'minor' => true,
+ 'visibility' => RevisionRecord::DELETED_RESTRICTED,
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider provideInsertRevisionOn_successes
+ * @covers RevisionStore::insertRevisionOn
+ */
+ public function testInsertRevisionOn_successes( Title $title, array $revDetails = [] ) {
+ $rev = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
+
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+ $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER ) );
+
+ $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
+ $this->assertRevisionRecordsEqual( $rev, $return );
+ }
+
+ /**
+ * @covers RevisionStore::insertRevisionOn
+ */
+ public function testInsertRevisionOn_blobAddressExists() {
+ $title = Title::newFromText( 'UTPage' );
+ $revDetails = [
+ 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
+ 'parent' => true,
+ 'comment' => $this->getRandomCommentStoreComment(),
+ 'timestamp' => '20171117010101',
+ 'user' => true,
+ ];
+
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+
+ // Insert the first revision
+ $revOne = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
+ $firstReturn = $store->insertRevisionOn( $revOne, wfGetDB( DB_MASTER ) );
+ $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() );
+ $this->assertRevisionRecordsEqual( $revOne, $firstReturn );
+
+ // Insert a second revision inheriting the same blob address
+ $revDetails['slot'] = SlotRecord::newInherited( $firstReturn->getSlot( 'main' ) );
+ $revTwo = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
+ $secondReturn = $store->insertRevisionOn( $revTwo, wfGetDB( DB_MASTER ) );
+ $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() );
+ $this->assertRevisionRecordsEqual( $revTwo, $secondReturn );
+
+ // Assert that the same blob address has been used.
+ $this->assertEquals(
+ $firstReturn->getSlot( 'main' )->getAddress(),
+ $secondReturn->getSlot( 'main' )->getAddress()
+ );
+ // And that different revisions have been created.
+ $this->assertNotSame(
+ $firstReturn->getId(),
+ $secondReturn->getId()
+ );
+ }
+
+ public function provideInsertRevisionOn_failures() {
+ yield 'no slot' => [
+ Title::newFromText( 'UTPage' ),
+ [
+ 'comment' => $this->getRandomCommentStoreComment(),
+ 'timestamp' => '20171117010101',
+ 'user' => true,
+ ],
+ new InvalidArgumentException( 'At least one slot needs to be defined!' )
+ ];
+ yield 'slot that is not main slot' => [
+ Title::newFromText( 'UTPage' ),
+ [
+ 'slot' => SlotRecord::newUnsaved( 'lalala', new WikitextContent( 'Chicken' ) ),
+ 'comment' => $this->getRandomCommentStoreComment(),
+ 'timestamp' => '20171117010101',
+ 'user' => true,
+ ],
+ new InvalidArgumentException( 'Only the main slot is supported for now!' )
+ ];
+ yield 'no timestamp' => [
+ Title::newFromText( 'UTPage' ),
+ [
+ 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
+ 'comment' => $this->getRandomCommentStoreComment(),
+ 'user' => true,
+ ],
+ new IncompleteRevisionException( 'timestamp field must not be NULL!' )
+ ];
+ yield 'no comment' => [
+ Title::newFromText( 'UTPage' ),
+ [
+ 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
+ 'timestamp' => '20171117010101',
+ 'user' => true,
+ ],
+ new IncompleteRevisionException( 'comment must not be NULL!' )
+ ];
+ yield 'no user' => [
+ Title::newFromText( 'UTPage' ),
+ [
+ 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
+ 'comment' => $this->getRandomCommentStoreComment(),
+ 'timestamp' => '20171117010101',
+ ],
+ new IncompleteRevisionException( 'user must not be NULL!' )
+ ];
+ }
+
+ /**
+ * @dataProvider provideInsertRevisionOn_failures
+ * @covers RevisionStore::insertRevisionOn
+ */
+ public function testInsertRevisionOn_failures(
+ Title $title,
+ array $revDetails = [],
+ Exception $exception ) {
+ $rev = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
+
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+
+ $this->setExpectedException(
+ get_class( $exception ),
+ $exception->getMessage(),
+ $exception->getCode()
+ );
+ $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER ) );
+ }
+
+ public function provideNewNullRevision() {
+ yield [
+ Title::newFromText( 'UTPage' ),
+ CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment1' ),
+ true,
+ ];
+ yield [
+ Title::newFromText( 'UTPage' ),
+ CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment2', [ 'a' => 1 ] ),
+ false,
+ ];
+ }
+
+ /**
+ * @dataProvider provideNewNullRevision
+ * @covers RevisionStore::newNullRevision
+ */
+ public function testNewNullRevision( Title $title, $comment, $minor ) {
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+ $user = TestUserRegistry::getMutableTestUser( __METHOD__ )->getUser();
+ $record = $store->newNullRevision(
+ wfGetDB( DB_MASTER ),
+ $title,
+ $comment,
+ $minor,
+ $user
+ );
+
+ $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
+ $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
+ $this->assertEquals( $comment, $record->getComment() );
+ $this->assertEquals( $minor, $record->isMinor() );
+ $this->assertEquals( $user->getName(), $record->getUser()->getName() );
+ }
+
+ /**
+ * @covers RevisionStore::newNullRevision
+ */
+ public function testNewNullRevision_nonExistingTitle() {
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+ $record = $store->newNullRevision(
+ wfGetDB( DB_MASTER ),
+ Title::newFromText( __METHOD__ . '.iDontExist!' ),
+ CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment' ),
+ false,
+ TestUserRegistry::getMutableTestUser( __METHOD__ )->getUser()
+ );
+ $this->assertNull( $record );
+ }
+
+ /**
+ * @covers RevisionStore::isUnpatrolled
+ */
+ public function testIsUnpatrolled_returnsRecentChangesId() {
+ $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
+ $status = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
+ /** @var Revision $rev */
+ $rev = $status->value['revision'];
+
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+ $revisionRecord = $store->getRevisionById( $rev->getId() );
+ $result = $store->isUnpatrolled( $revisionRecord );
+
+ $this->assertGreaterThan( 0, $result );
+ $this->assertSame(
+ $page->getRevision()->getRecentChange()->getAttribute( 'rc_id' ),
+ $result
+ );
+ }
+
+ /**
+ * @covers RevisionStore::isUnpatrolled
+ */
+ public function testIsUnpatrolled_returnsZeroIfPatrolled() {
+ // This assumes that sysops are auto patrolled
+ $sysop = $this->getTestSysop()->getUser();
+ $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
+ $status = $page->doEditContent(
+ new WikitextContent( __METHOD__ ),
+ __METHOD__,
+ 0,
+ false,
+ $sysop
+ );
+ /** @var Revision $rev */
+ $rev = $status->value['revision'];
+
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+ $revisionRecord = $store->getRevisionById( $rev->getId() );
+ $result = $store->isUnpatrolled( $revisionRecord );
+
+ $this->assertSame( 0, $result );
+ }
+
+ public function testGetRecentChange() {
+ $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
+ $content = new WikitextContent( __METHOD__ );
+ $status = $page->doEditContent( $content, __METHOD__ );
+ /** @var Revision $rev */
+ $rev = $status->value['revision'];
+
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+ $revRecord = $store->getRevisionById( $rev->getId() );
+ $recentChange = $store->getRecentChange( $revRecord );
+
+ $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
+ $this->assertEquals( $rev->getRecentChange(), $recentChange );
+ }
+
+ /**
+ * @covers RevisionStore::getRevisionById
+ */
+ public function testGetRevisionById() {
+ $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
+ $content = new WikitextContent( __METHOD__ );
+ $status = $page->doEditContent( $content, __METHOD__ );
+ /** @var Revision $rev */
+ $rev = $status->value['revision'];
+
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+ $revRecord = $store->getRevisionById( $rev->getId() );
+
+ $this->assertSame( $rev->getId(), $revRecord->getId() );
+ $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
+ $this->assertSame( __METHOD__, $revRecord->getComment()->text );
+ }
+
+ /**
+ * @covers RevisionStore::getRevisionByTitle
+ */
+ public function testGetRevisionByTitle() {
+ $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
+ $content = new WikitextContent( __METHOD__ );
+ $status = $page->doEditContent( $content, __METHOD__ );
+ /** @var Revision $rev */
+ $rev = $status->value['revision'];
+
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+ $revRecord = $store->getRevisionByTitle( $page->getTitle() );
+
+ $this->assertSame( $rev->getId(), $revRecord->getId() );
+ $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
+ $this->assertSame( __METHOD__, $revRecord->getComment()->text );
+ }
+
+ /**
+ * @covers RevisionStore::getRevisionByPageId
+ */
+ public function testGetRevisionByPageId() {
+ $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
+ $content = new WikitextContent( __METHOD__ );
+ $status = $page->doEditContent( $content, __METHOD__ );
+ /** @var Revision $rev */
+ $rev = $status->value['revision'];
+
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+ $revRecord = $store->getRevisionByPageId( $page->getId() );
+
+ $this->assertSame( $rev->getId(), $revRecord->getId() );
+ $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
+ $this->assertSame( __METHOD__, $revRecord->getComment()->text );
+ }
+
+ /**
+ * @covers RevisionStore::getRevisionFromTimestamp
+ */
+ public function testGetRevisionFromTimestamp() {
+ // Make sure there is 1 second between the last revision and the rev we create...
+ // Otherwise we might not get the correct revision and the test may fail...
+ // :(
+ sleep( 1 );
+ $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
+ $content = new WikitextContent( __METHOD__ );
+ $status = $page->doEditContent( $content, __METHOD__ );
+ /** @var Revision $rev */
+ $rev = $status->value['revision'];
+
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+ $revRecord = $store->getRevisionFromTimestamp(
+ $page->getTitle(),
+ $rev->getTimestamp()
+ );
+
+ $this->assertSame( $rev->getId(), $revRecord->getId() );
+ $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
+ $this->assertSame( __METHOD__, $revRecord->getComment()->text );
+ }
+
+ private function revisionToRow( Revision $rev ) {
+ $page = WikiPage::factory( $rev->getTitle() );
+
+ return (object)[
+ 'rev_id' => (string)$rev->getId(),
+ 'rev_page' => (string)$rev->getPage(),
+ 'rev_text_id' => (string)$rev->getTextId(),
+ 'rev_timestamp' => (string)$rev->getTimestamp(),
+ 'rev_user_text' => (string)$rev->getUserText(),
+ 'rev_user' => (string)$rev->getUser(),
+ 'rev_minor_edit' => $rev->isMinor() ? '1' : '0',
+ 'rev_deleted' => (string)$rev->getVisibility(),
+ 'rev_len' => (string)$rev->getSize(),
+ 'rev_parent_id' => (string)$rev->getParentId(),
+ 'rev_sha1' => (string)$rev->getSha1(),
+ 'rev_comment_text' => $rev->getComment(),
+ 'rev_comment_data' => null,
+ 'rev_comment_cid' => null,
+ 'rev_content_format' => $rev->getContentFormat(),
+ 'rev_content_model' => $rev->getContentModel(),
+ 'page_namespace' => (string)$page->getTitle()->getNamespace(),
+ 'page_title' => $page->getTitle()->getDBkey(),
+ 'page_id' => (string)$page->getId(),
+ 'page_latest' => (string)$page->getLatest(),
+ 'page_is_redirect' => $page->isRedirect() ? '1' : '0',
+ 'page_len' => (string)$page->getContent()->getSize(),
+ 'user_name' => (string)$rev->getUserText(),
+ ];
+ }
+
+ private function assertRevisionRecordMatchesRevision(
+ Revision $rev,
+ RevisionRecord $record
+ ) {
+ $this->assertSame( $rev->getId(), $record->getId() );
+ $this->assertSame( $rev->getPage(), $record->getPageId() );
+ $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
+ $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
+ $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
+ $this->assertSame( $rev->isMinor(), $record->isMinor() );
+ $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
+ $this->assertSame( $rev->getSize(), $record->getSize() );
+ /**
+ * @note As of MW 1.31, the database schema allows the parent ID to be
+ * NULL to indicate that it is unknown.
+ */
+ $expectedParent = $rev->getParentId();
+ if ( $expectedParent === null ) {
+ $expectedParent = 0;
+ }
+ $this->assertSame( $expectedParent, $record->getParentId() );
+ $this->assertSame( $rev->getSha1(), $record->getSha1() );
+ $this->assertSame( $rev->getComment(), $record->getComment()->text );
+ $this->assertSame( $rev->getContentFormat(), $record->getContent( 'main' )->getDefaultFormat() );
+ $this->assertSame( $rev->getContentModel(), $record->getContent( 'main' )->getModel() );
+ $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
+ }
+
+ /**
+ * @covers RevisionStore::newRevisionFromRow
+ * @covers RevisionStore::newRevisionFromRow_1_29
+ */
+ public function testNewRevisionFromRow_anonEdit() {
+ $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
+ /** @var Revision $rev */
+ $rev = $page->doEditContent(
+ new WikitextContent( __METHOD__. 'a' ),
+ __METHOD__. 'a'
+ )->value['revision'];
+
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+ $record = $store->newRevisionFromRow(
+ $this->revisionToRow( $rev ),
+ [],
+ $page->getTitle()
+ );
+ $this->assertRevisionRecordMatchesRevision( $rev, $record );
+ }
+
+ /**
+ * @covers RevisionStore::newRevisionFromRow
+ * @covers RevisionStore::newRevisionFromRow_1_29
+ */
+ public function testNewRevisionFromRow_userEdit() {
+ $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
+ /** @var Revision $rev */
+ $rev = $page->doEditContent(
+ new WikitextContent( __METHOD__. 'b' ),
+ __METHOD__ . 'b',
+ 0,
+ false,
+ $this->getTestUser()->getUser()
+ )->value['revision'];
+
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+ $record = $store->newRevisionFromRow(
+ $this->revisionToRow( $rev ),
+ [],
+ $page->getTitle()
+ );
+ $this->assertRevisionRecordMatchesRevision( $rev, $record );
+ }
+
+ /**
+ * @covers RevisionStore::newRevisionFromArchiveRow
+ */
+ public function testNewRevisionFromArchiveRow() {
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+ $title = Title::newFromText( __METHOD__ );
+ $page = WikiPage::factory( $title );
+ /** @var Revision $orig */
+ $orig = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
+ ->value['revision'];
+ $page->doDeleteArticle( __METHOD__ );
+
+ $db = wfGetDB( DB_MASTER );
+ $arQuery = $store->getArchiveQueryInfo();
+ $res = $db->select(
+ $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
+ __METHOD__, [], $arQuery['joins']
+ );
+ $this->assertTrue( is_object( $res ), 'query failed' );
+
+ $row = $res->fetchObject();
+ $res->free();
+ $record = $store->newRevisionFromArchiveRow( $row );
+
+ $this->assertRevisionRecordMatchesRevision( $orig, $record );
+ }
+
+ /**
+ * @covers RevisionStore::loadRevisionFromId
+ */
+ public function testLoadRevisionFromId() {
+ $title = Title::newFromText( __METHOD__ );
+ $page = WikiPage::factory( $title );
+ /** @var Revision $rev */
+ $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
+ ->value['revision'];
+
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+ $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER ), $rev->getId() );
+ $this->assertRevisionRecordMatchesRevision( $rev, $result );
+ }
+
+ /**
+ * @covers RevisionStore::loadRevisionFromPageId
+ */
+ public function testLoadRevisionFromPageId() {
+ $title = Title::newFromText( __METHOD__ );
+ $page = WikiPage::factory( $title );
+ /** @var Revision $rev */
+ $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
+ ->value['revision'];
+
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+ $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER ), $page->getId() );
+ $this->assertRevisionRecordMatchesRevision( $rev, $result );
+ }
+
+ /**
+ * @covers RevisionStore::loadRevisionFromTitle
+ */
+ public function testLoadRevisionFromTitle() {
+ $title = Title::newFromText( __METHOD__ );
+ $page = WikiPage::factory( $title );
+ /** @var Revision $rev */
+ $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
+ ->value['revision'];
+
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+ $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER ), $title );
+ $this->assertRevisionRecordMatchesRevision( $rev, $result );
+ }
+
+ /**
+ * @covers RevisionStore::loadRevisionFromTimestamp
+ */
+ public function testLoadRevisionFromTimestamp() {
+ $title = Title::newFromText( __METHOD__ );
+ $page = WikiPage::factory( $title );
+ /** @var Revision $revOne */
+ $revOne = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
+ ->value['revision'];
+ // Sleep to ensure different timestamps... )(evil)
+ sleep( 1 );
+ /** @var Revision $revTwo */
+ $revTwo = $page->doEditContent( new WikitextContent( __METHOD__ . 'a' ), '' )
+ ->value['revision'];
+
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+ $this->assertNull(
+ $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER ), $title, '20150101010101' )
+ );
+ $this->assertSame(
+ $revOne->getId(),
+ $store->loadRevisionFromTimestamp(
+ wfGetDB( DB_MASTER ),
+ $title,
+ $revOne->getTimestamp()
+ )->getId()
+ );
+ $this->assertSame(
+ $revTwo->getId(),
+ $store->loadRevisionFromTimestamp(
+ wfGetDB( DB_MASTER ),
+ $title,
+ $revTwo->getTimestamp()
+ )->getId()
+ );
+ }
+
+ /**
+ * @covers RevisionStore::listRevisionSizes
+ */
+ public function testGetParentLengths() {
+ $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
+ /** @var Revision $revOne */
+ $revOne = $page->doEditContent(
+ new WikitextContent( __METHOD__ ), __METHOD__
+ )->value['revision'];
+ /** @var Revision $revTwo */
+ $revTwo = $page->doEditContent(
+ new WikitextContent( __METHOD__ . '2' ), __METHOD__
+ )->value['revision'];
+
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+ $this->assertSame(
+ [
+ $revOne->getId() => strlen( __METHOD__ ),
+ ],
+ $store->listRevisionSizes(
+ wfGetDB( DB_MASTER ),
+ [ $revOne->getId() ]
+ )
+ );
+ $this->assertSame(
+ [
+ $revOne->getId() => strlen( __METHOD__ ),
+ $revTwo->getId() => strlen( __METHOD__ ) + 1,
+ ],
+ $store->listRevisionSizes(
+ wfGetDB( DB_MASTER ),
+ [ $revOne->getId(), $revTwo->getId() ]
+ )
+ );
+ }
+
+ /**
+ * @covers RevisionStore::getPreviousRevision
+ */
+ public function testGetPreviousRevision() {
+ $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
+ /** @var Revision $revOne */
+ $revOne = $page->doEditContent(
+ new WikitextContent( __METHOD__ ), __METHOD__
+ )->value['revision'];
+ /** @var Revision $revTwo */
+ $revTwo = $page->doEditContent(
+ new WikitextContent( __METHOD__ . '2' ), __METHOD__
+ )->value['revision'];
+
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+ $this->assertNull(
+ $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
+ );
+ $this->assertSame(
+ $revOne->getId(),
+ $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
+ );
+ }
+
+ /**
+ * @covers RevisionStore::getNextRevision
+ */
+ public function testGetNextRevision() {
+ $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
+ /** @var Revision $revOne */
+ $revOne = $page->doEditContent(
+ new WikitextContent( __METHOD__ ), __METHOD__
+ )->value['revision'];
+ /** @var Revision $revTwo */
+ $revTwo = $page->doEditContent(
+ new WikitextContent( __METHOD__ . '2' ), __METHOD__
+ )->value['revision'];
+
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+ $this->assertSame(
+ $revTwo->getId(),
+ $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
+ );
+ $this->assertNull(
+ $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
+ );
+ }
+
+ /**
+ * @covers RevisionStore::getTimestampFromId
+ */
+ public function testGetTimestampFromId_found() {
+ $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
+ /** @var Revision $rev */
+ $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
+ ->value['revision'];
+
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+ $result = $store->getTimestampFromId(
+ $page->getTitle(),
+ $rev->getId()
+ );
+
+ $this->assertSame( $rev->getTimestamp(), $result );
+ }
+
+ /**
+ * @covers RevisionStore::getTimestampFromId
+ */
+ public function testGetTimestampFromId_notFound() {
+ $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
+ /** @var Revision $rev */
+ $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ )
+ ->value['revision'];
+
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+ $result = $store->getTimestampFromId(
+ $page->getTitle(),
+ $rev->getId() + 1
+ );
+
+ $this->assertFalse( $result );
+ }
+
+ /**
+ * @covers RevisionStore::countRevisionsByPageId
+ */
+ public function testCountRevisionsByPageId() {
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+ $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
+
+ $this->assertSame(
+ 0,
+ $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
+ );
+ $page->doEditContent( new WikitextContent( 'a' ), 'a' );
+ $this->assertSame(
+ 1,
+ $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
+ );
+ $page->doEditContent( new WikitextContent( 'b' ), 'b' );
+ $this->assertSame(
+ 2,
+ $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() )
+ );
+ }
+
+ /**
+ * @covers RevisionStore::countRevisionsByTitle
+ */
+ public function testCountRevisionsByTitle() {
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+ $page = WikiPage::factory( Title::newFromText( __METHOD__ ) );
+
+ $this->assertSame(
+ 0,
+ $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
+ );
+ $page->doEditContent( new WikitextContent( 'a' ), 'a' );
+ $this->assertSame(
+ 1,
+ $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
+ );
+ $page->doEditContent( new WikitextContent( 'b' ), 'b' );
+ $this->assertSame(
+ 2,
+ $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() )
+ );
+ }
+
+ /**
+ * @covers RevisionStore::userWasLastToEdit
+ */
+ public function testUserWasLastToEdit_false() {
+ $sysop = $this->getTestSysop()->getUser();
+ $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
+ $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
+
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+ $result = $store->userWasLastToEdit(
+ wfGetDB( DB_MASTER ),
+ $page->getId(),
+ $sysop->getId(),
+ '20160101010101'
+ );
+ $this->assertFalse( $result );
+ }
+
+ /**
+ * @covers RevisionStore::userWasLastToEdit
+ */
+ public function testUserWasLastToEdit_true() {
+ $startTime = wfTimestampNow();
+ $sysop = $this->getTestSysop()->getUser();
+ $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
+ $page->doEditContent(
+ new WikitextContent( __METHOD__ ),
+ __METHOD__,
+ 0,
+ false,
+ $sysop
+ );
+
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+ $result = $store->userWasLastToEdit(
+ wfGetDB( DB_MASTER ),
+ $page->getId(),
+ $sysop->getId(),
+ $startTime
+ );
+ $this->assertTrue( $result );
+ }
+
+ /**
+ * @covers RevisionStore::getKnownCurrentRevision
+ */
+ public function testGetKnownCurrentRevision() {
+ $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
+ /** @var Revision $rev */
+ $rev = $page->doEditContent(
+ new WikitextContent( __METHOD__. 'b' ),
+ __METHOD__ . 'b',
+ 0,
+ false,
+ $this->getTestUser()->getUser()
+ )->value['revision'];
+
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+ $record = $store->getKnownCurrentRevision(
+ $page->getTitle(),
+ $rev->getId()
+ );
+
+ $this->assertRevisionRecordMatchesRevision( $rev, $record );
+ }
+
+ public function provideNewMutableRevisionFromArray() {
+ yield 'Basic array, with page & id' => [
+ [
+ 'id' => 2,
+ 'page' => 1,
+ 'text_id' => 2,
+ 'timestamp' => '20171017114835',
+ 'user_text' => '111.0.1.2',
+ 'user' => 0,
+ 'minor_edit' => false,
+ 'deleted' => 0,
+ 'len' => 46,
+ 'parent_id' => 1,
+ 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
+ 'comment' => 'Goat Comment!',
+ 'content_format' => 'text/x-wiki',
+ 'content_model' => 'wikitext',
+ ]
+ ];
+ yield 'Basic array, content object' => [
+ [
+ 'id' => 2,
+ 'page' => 1,
+ 'timestamp' => '20171017114835',
+ 'user_text' => '111.0.1.2',
+ 'user' => 0,
+ 'minor_edit' => false,
+ 'deleted' => 0,
+ 'len' => 46,
+ 'parent_id' => 1,
+ 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
+ 'comment' => 'Goat Comment!',
+ 'content' => new WikitextContent( 'Some Content' ),
+ ]
+ ];
+ yield 'Basic array, with title' => [
+ [
+ 'title' => Title::newFromText( 'SomeText' ),
+ 'text_id' => 2,
+ 'timestamp' => '20171017114835',
+ 'user_text' => '111.0.1.2',
+ 'user' => 0,
+ 'minor_edit' => false,
+ 'deleted' => 0,
+ 'len' => 46,
+ 'parent_id' => 1,
+ 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
+ 'comment' => 'Goat Comment!',
+ 'content_format' => 'text/x-wiki',
+ 'content_model' => 'wikitext',
+ ]
+ ];
+ yield 'Basic array, no user field' => [
+ [
+ 'id' => 2,
+ 'page' => 1,
+ 'text_id' => 2,
+ 'timestamp' => '20171017114835',
+ 'user_text' => '111.0.1.3',
+ 'minor_edit' => false,
+ 'deleted' => 0,
+ 'len' => 46,
+ 'parent_id' => 1,
+ 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
+ 'comment' => 'Goat Comment!',
+ 'content_format' => 'text/x-wiki',
+ 'content_model' => 'wikitext',
+ ]
+ ];
+ }
+
+ /**
+ * @dataProvider provideNewMutableRevisionFromArray
+ * @covers RevisionStore::newMutableRevisionFromArray
+ */
+ public function testNewMutableRevisionFromArray( array $array ) {
+ $store = MediaWikiServices::getInstance()->getRevisionStore();
+
+ $result = $store->newMutableRevisionFromArray( $array );
+
+ if ( isset( $array['id'] ) ) {
+ $this->assertSame( $array['id'], $result->getId() );
+ }
+ if ( isset( $array['page'] ) ) {
+ $this->assertSame( $array['page'], $result->getPageId() );
+ }
+ $this->assertSame( $array['timestamp'], $result->getTimestamp() );
+ $this->assertSame( $array['user_text'], $result->getUser()->getName() );
+ if ( isset( $array['user'] ) ) {
+ $this->assertSame( $array['user'], $result->getUser()->getId() );
+ }
+ $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
+ $this->assertSame( $array['deleted'], $result->getVisibility() );
+ $this->assertSame( $array['len'], $result->getSize() );
+ $this->assertSame( $array['parent_id'], $result->getParentId() );
+ $this->assertSame( $array['sha1'], $result->getSha1() );
+ $this->assertSame( $array['comment'], $result->getComment()->text );
+ if ( isset( $array['content'] ) ) {
+ $this->assertTrue(
+ $result->getSlot( 'main' )->getContent()->equals( $array['content'] )
+ );
+ } else {
+ $this->assertSame(
+ $array['content_format'],
+ $result->getSlot( 'main' )->getContent()->getDefaultFormat()
+ );
+ $this->assertSame( $array['content_model'], $result->getSlot( 'main' )->getModel() );
+ }
+ }
+
+}
use Title;
/**
- * @covers MediaWiki\Storage\RevisionStoreRecord
+ * @covers \MediaWiki\Storage\RevisionStoreRecord
*/
class RevisionStoreRecordTest extends MediaWikiTestCase {
/**
- * @param array $overrides
+ * @param array $rowOverrides
+ *
* @return RevisionStoreRecord
*/
- public function newRevision( array $overrides = [] ) {
+ public function newRevision( array $rowOverrides = [] ) {
$title = Title::newFromText( 'Dummy' );
$title->resetArticleID( 17 );
'page_latest' => '18',
];
- $row = array_merge( $row, $overrides );
+ $row = array_merge( $row, $rowOverrides );
return new RevisionStoreRecord( $title, $user, $comment, (object)$row, $slots );
}
);
}
- public function testHasSameContent() {
- // TBD
+ private function getSlotRecord( $role, $contentString ) {
+ return SlotRecord::newUnsaved( $role, new TextContent( $contentString ) );
}
- public function testIsDeleted() {
- // TBD
+ public function provideHasSameContent() {
+ /**
+ * @param SlotRecord[] $slots
+ * @param int $revId
+ * @return RevisionStoreRecord
+ */
+ $recordCreator = function ( array $slots, $revId ) {
+ $title = Title::newFromText( 'provideHasSameContent' );
+ $title->resetArticleID( 19 );
+ $slots = new RevisionSlots( $slots );
+
+ return new RevisionStoreRecord(
+ $title,
+ new UserIdentityValue( 11, __METHOD__ ),
+ CommentStoreComment::newUnsavedComment( __METHOD__ ),
+ (object)[
+ 'rev_id' => strval( $revId ),
+ 'rev_page' => strval( $title->getArticleID() ),
+ 'rev_timestamp' => '20200101000000',
+ 'rev_deleted' => 0,
+ 'rev_minor_edit' => 0,
+ 'rev_parent_id' => '5',
+ 'rev_len' => $slots->computeSize(),
+ 'rev_sha1' => $slots->computeSha1(),
+ 'page_latest' => '18',
+ ],
+ $slots
+ );
+ };
+
+ // Create some slots with content
+ $mainA = SlotRecord::newUnsaved( 'main', new TextContent( 'A' ) );
+ $mainB = SlotRecord::newUnsaved( 'main', new TextContent( 'B' ) );
+ $auxA = SlotRecord::newUnsaved( 'aux', new TextContent( 'A' ) );
+ $auxB = SlotRecord::newUnsaved( 'aux', new TextContent( 'A' ) );
+
+ $initialRecord = $recordCreator( [ $mainA ], 12 );
+
+ return [
+ 'same record object' => [
+ true,
+ $initialRecord,
+ $initialRecord,
+ ],
+ 'same record content, different object' => [
+ true,
+ $recordCreator( [ $mainA ], 12 ),
+ $recordCreator( [ $mainA ], 13 ),
+ ],
+ 'same record content, aux slot, different object' => [
+ true,
+ $recordCreator( [ $auxA ], 12 ),
+ $recordCreator( [ $auxB ], 13 ),
+ ],
+ 'different content' => [
+ false,
+ $recordCreator( [ $mainA ], 12 ),
+ $recordCreator( [ $mainB ], 13 ),
+ ],
+ 'different content and number of slots' => [
+ false,
+ $recordCreator( [ $mainA ], 12 ),
+ $recordCreator( [ $mainA, $mainB ], 13 ),
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider provideHasSameContent
+ * @covers RevisionRecord::hasSameContent
+ * @group Database
+ */
+ public function testHasSameContent(
+ $expected,
+ RevisionRecord $record1,
+ RevisionRecord $record2
+ ) {
+ $this->assertSame(
+ $expected,
+ $record1->hasSameContent( $record2 )
+ );
+ }
+
+ public function provideIsDeleted() {
+ yield 'no deletion' => [
+ 0,
+ [
+ RevisionRecord::DELETED_TEXT => false,
+ RevisionRecord::DELETED_COMMENT => false,
+ RevisionRecord::DELETED_USER => false,
+ RevisionRecord::DELETED_RESTRICTED => false,
+ ]
+ ];
+ yield 'text deleted' => [
+ RevisionRecord::DELETED_TEXT,
+ [
+ RevisionRecord::DELETED_TEXT => true,
+ RevisionRecord::DELETED_COMMENT => false,
+ RevisionRecord::DELETED_USER => false,
+ RevisionRecord::DELETED_RESTRICTED => false,
+ ]
+ ];
+ yield 'text and comment deleted' => [
+ RevisionRecord::DELETED_TEXT + RevisionRecord::DELETED_COMMENT,
+ [
+ RevisionRecord::DELETED_TEXT => true,
+ RevisionRecord::DELETED_COMMENT => true,
+ RevisionRecord::DELETED_USER => false,
+ RevisionRecord::DELETED_RESTRICTED => false,
+ ]
+ ];
+ yield 'all 4 deleted' => [
+ RevisionRecord::DELETED_TEXT +
+ RevisionRecord::DELETED_COMMENT +
+ RevisionRecord::DELETED_RESTRICTED +
+ RevisionRecord::DELETED_USER,
+ [
+ RevisionRecord::DELETED_TEXT => true,
+ RevisionRecord::DELETED_COMMENT => true,
+ RevisionRecord::DELETED_USER => true,
+ RevisionRecord::DELETED_RESTRICTED => true,
+ ]
+ ];
}
- public function testUserCan() {
- // TBD
+ /**
+ * @dataProvider provideIsDeleted
+ * @covers RevisionRecord::isDeleted
+ */
+ public function testIsDeleted( $revDeleted, $assertionMap ) {
+ $rev = $this->newRevision( [ 'rev_deleted' => $revDeleted ] );
+ foreach ( $assertionMap as $deletionLevel => $expected ) {
+ $this->assertSame( $expected, $rev->isDeleted( $deletionLevel ) );
+ }
}
}
--- /dev/null
+<?php
+
+namespace MediaWiki\Tests\Storage;
+
+use MediaWiki\Storage\RevisionStore;
+use MediaWiki\Storage\SqlBlobStore;
+use MediaWikiTestCase;
+use WANObjectCache;
+use Wikimedia\Rdbms\LoadBalancer;
+
+class RevisionStoreTest extends MediaWikiTestCase {
+
+ /**
+ * @param LoadBalancer $loadBalancer
+ * @param SqlBlobStore $blobStore
+ * @param WANObjectCache $WANObjectCache
+ *
+ * @return RevisionStore
+ */
+ private function getRevisionStore(
+ $loadBalancer = null,
+ $blobStore = null,
+ $WANObjectCache = null
+ ) {
+ return new RevisionStore(
+ $loadBalancer ? $loadBalancer : $this->getMockLoadBalancer(),
+ $blobStore ? $blobStore : $this->getMockSqlBlobStore(),
+ $WANObjectCache ? $WANObjectCache : $this->getHashWANObjectCache()
+ );
+ }
+
+ /**
+ * @return \PHPUnit_Framework_MockObject_MockObject|LoadBalancer
+ */
+ private function getMockLoadBalancer() {
+ return $this->getMockBuilder( LoadBalancer::class )
+ ->disableOriginalConstructor()->getMock();
+ }
+
+ /**
+ * @return \PHPUnit_Framework_MockObject_MockObject|SqlBlobStore
+ */
+ private function getMockSqlBlobStore() {
+ return $this->getMockBuilder( SqlBlobStore::class )
+ ->disableOriginalConstructor()->getMock();
+ }
+
+ private function getHashWANObjectCache() {
+ return new WANObjectCache( [ 'cache' => new \HashBagOStuff() ] );
+ }
+
+ /**
+ * @covers \MediaWiki\Storage\RevisionStore::getContentHandlerUseDB
+ * @covers \MediaWiki\Storage\RevisionStore::setContentHandlerUseDB
+ */
+ public function testGetSetContentHandlerDb() {
+ $store = $this->getRevisionStore();
+ $this->assertTrue( $store->getContentHandlerUseDB() );
+ $store->setContentHandlerUseDB( false );
+ $this->assertFalse( $store->getContentHandlerUseDB() );
+ $store->setContentHandlerUseDB( true );
+ $this->assertTrue( $store->getContentHandlerUseDB() );
+ }
+
+ private function getDefaultQueryFields() {
+ return [
+ 'rev_id',
+ 'rev_page',
+ 'rev_text_id',
+ 'rev_timestamp',
+ 'rev_user_text',
+ 'rev_user',
+ 'rev_minor_edit',
+ 'rev_deleted',
+ 'rev_len',
+ 'rev_parent_id',
+ 'rev_sha1',
+ ];
+ }
+
+ private function getCommentQueryFields() {
+ return [
+ 'rev_comment_text' => 'rev_comment',
+ 'rev_comment_data' => 'NULL',
+ 'rev_comment_cid' => 'NULL',
+ ];
+ }
+
+ private function getContentHandlerQueryFields() {
+ return [
+ 'rev_content_format',
+ 'rev_content_model',
+ ];
+ }
+
+ public function provideGetQueryInfo() {
+ yield [
+ true,
+ [],
+ [
+ 'tables' => [ 'revision' ],
+ 'fields' => array_merge(
+ $this->getDefaultQueryFields(),
+ $this->getCommentQueryFields(),
+ $this->getContentHandlerQueryFields()
+ ),
+ 'joins' => [],
+ ]
+ ];
+ yield [
+ false,
+ [],
+ [
+ 'tables' => [ 'revision' ],
+ 'fields' => array_merge(
+ $this->getDefaultQueryFields(),
+ $this->getCommentQueryFields()
+ ),
+ 'joins' => [],
+ ]
+ ];
+ yield [
+ false,
+ [ 'page' ],
+ [
+ 'tables' => [ 'revision', 'page' ],
+ 'fields' => array_merge(
+ $this->getDefaultQueryFields(),
+ $this->getCommentQueryFields(),
+ [
+ 'page_namespace',
+ 'page_title',
+ 'page_id',
+ 'page_latest',
+ 'page_is_redirect',
+ 'page_len',
+ ]
+ ),
+ 'joins' => [
+ 'page' => [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
+ ],
+ ]
+ ];
+ yield [
+ false,
+ [ 'user' ],
+ [
+ 'tables' => [ 'revision', 'user' ],
+ 'fields' => array_merge(
+ $this->getDefaultQueryFields(),
+ $this->getCommentQueryFields(),
+ [
+ 'user_name',
+ ]
+ ),
+ 'joins' => [
+ 'user' => [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
+ ],
+ ]
+ ];
+ yield [
+ false,
+ [ 'text' ],
+ [
+ 'tables' => [ 'revision', 'text' ],
+ 'fields' => array_merge(
+ $this->getDefaultQueryFields(),
+ $this->getCommentQueryFields(),
+ [
+ 'old_text',
+ 'old_flags',
+ ]
+ ),
+ 'joins' => [
+ 'text' => [ 'INNER JOIN', [ 'rev_text_id=old_id' ] ],
+ ],
+ ]
+ ];
+ yield [
+ true,
+ [ 'page', 'user', 'text' ],
+ [
+ 'tables' => [ 'revision', 'page', 'user', 'text' ],
+ 'fields' => array_merge(
+ $this->getDefaultQueryFields(),
+ $this->getCommentQueryFields(),
+ $this->getContentHandlerQueryFields(),
+ [
+ 'page_namespace',
+ 'page_title',
+ 'page_id',
+ 'page_latest',
+ 'page_is_redirect',
+ 'page_len',
+ 'user_name',
+ 'old_text',
+ 'old_flags',
+ ]
+ ),
+ 'joins' => [
+ 'page' => [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
+ 'user' => [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
+ 'text' => [ 'INNER JOIN', [ 'rev_text_id=old_id' ] ],
+ ],
+ ]
+ ];
+ }
+
+ /**
+ * @dataProvider provideGetQueryInfo
+ * @covers \MediaWiki\Storage\RevisionStore::getQueryInfo
+ */
+ public function testGetQueryInfo( $contentHandlerUseDb, $options, $expected ) {
+ $store = $this->getRevisionStore();
+ $store->setContentHandlerUseDB( $contentHandlerUseDb );
+ $this->assertEquals( $expected, $store->getQueryInfo( $options ) );
+ }
+
+ private function getDefaultArchiveFields() {
+ return [
+ 'ar_id',
+ 'ar_page_id',
+ 'ar_namespace',
+ 'ar_title',
+ 'ar_rev_id',
+ 'ar_text',
+ 'ar_text_id',
+ 'ar_timestamp',
+ 'ar_user_text',
+ 'ar_user',
+ 'ar_minor_edit',
+ 'ar_deleted',
+ 'ar_len',
+ 'ar_parent_id',
+ 'ar_sha1',
+ ];
+ }
+
+ /**
+ * @covers \MediaWiki\Storage\RevisionStore::getArchiveQueryInfo
+ */
+ public function testGetArchiveQueryInfo_contentHandlerDb() {
+ $store = $this->getRevisionStore();
+ $store->setContentHandlerUseDB( true );
+ $this->assertEquals(
+ [
+ 'tables' => [
+ 'archive'
+ ],
+ 'fields' => array_merge(
+ $this->getDefaultArchiveFields(),
+ [
+ 'ar_comment_text' => 'ar_comment',
+ 'ar_comment_data' => 'NULL',
+ 'ar_comment_cid' => 'NULL',
+ 'ar_content_format',
+ 'ar_content_model',
+ ]
+ ),
+ 'joins' => [],
+ ],
+ $store->getArchiveQueryInfo()
+ );
+ }
+
+ /**
+ * @covers \MediaWiki\Storage\RevisionStore::getArchiveQueryInfo
+ */
+ public function testGetArchiveQueryInfo_noContentHandlerDb() {
+ $store = $this->getRevisionStore();
+ $store->setContentHandlerUseDB( false );
+ $this->assertEquals(
+ [
+ 'tables' => [
+ 'archive'
+ ],
+ 'fields' => array_merge(
+ $this->getDefaultArchiveFields(),
+ [
+ 'ar_comment_text' => 'ar_comment',
+ 'ar_comment_data' => 'NULL',
+ 'ar_comment_cid' => 'NULL',
+ ]
+ ),
+ 'joins' => [],
+ ],
+ $store->getArchiveQueryInfo()
+ );
+ }
+
+}
--- /dev/null
+<?php
+
+namespace MediaWiki\Tests\Storage;
+
+use MediaWiki\Storage\SlotRecord;
+use MediaWikiTestCase;
+use RuntimeException;
+use Wikimedia\Assert\ParameterTypeException;
+use WikitextContent;
+
+/**
+ * @covers \MediaWiki\Storage\SlotRecord
+ */
+class SlotRecordTest extends MediaWikiTestCase {
+
+ public function provideAContent() {
+ yield [ new WikitextContent( 'A' ) ];
+ yield [
+ function ( SlotRecord $slotRecord ) {
+ if ( $slotRecord->getAddress() === 'tt:456' ) {
+ return new WikitextContent( 'A' );
+ }
+ throw new RuntimeException( 'Got Wrong SlotRecord for callback' );
+ },
+ ];
+ }
+
+ /**
+ * @dataProvider provideAContent
+ */
+ public function testValidConstruction( $content ) {
+ $row = (object)[
+ 'cont_size' => '1',
+ 'cont_sha1' => 'someHash',
+ 'cont_address' => 'tt:456',
+ 'model_name' => 'aModelname',
+ 'slot_revision' => '2',
+ 'format_name' => 'someFormatName',
+ 'role_name' => 'myRole',
+ 'slot_inherited' => '99'
+ ];
+
+ $record = new SlotRecord( $row, $content );
+
+ $this->assertSame( 'A', $record->getContent()->getNativeData() );
+ $this->assertSame( 1, $record->getSize() );
+ $this->assertSame( 'someHash', $record->getSha1() );
+ $this->assertSame( 'aModelname', $record->getModel() );
+ $this->assertSame( 2, $record->getRevision() );
+ $this->assertSame( 'tt:456', $record->getAddress() );
+ $this->assertSame( 'someFormatName', $record->getFormat() );
+ $this->assertSame( 'myRole', $record->getRole() );
+ $this->assertTrue( $record->hasAddress() );
+ $this->assertTrue( $record->hasRevision() );
+ $this->assertTrue( $record->isInherited() );
+ }
+
+ public function provideInvalidConstruction() {
+ yield 'both null' => [ null, null ];
+ yield 'null row' => [ null, new WikitextContent( 'A' ) ];
+ yield 'array row' => [ null, new WikitextContent( 'A' ) ];
+ yield 'null content' => [ (object)[], null ];
+ }
+
+ /**
+ * @dataProvider provideInvalidConstruction
+ */
+ public function testInvalidConstruction( $row, $content ) {
+ $this->setExpectedException( ParameterTypeException::class );
+ new SlotRecord( $row, $content );
+ }
+
+ public function testHasAddress_false() {
+ $record = new SlotRecord( (object)[], new WikitextContent( 'A' ) );
+ $this->assertFalse( $record->hasAddress() );
+ }
+
+ public function testHasRevision_false() {
+ $record = new SlotRecord( (object)[], new WikitextContent( 'A' ) );
+ $this->assertFalse( $record->hasRevision() );
+ }
+
+ public function testInInherited_false() {
+ // TODO unskip me once fixed.
+ $this->markTestSkipped( 'Should probably return false, needs fixing?' );
+ $record = new SlotRecord( (object)[], new WikitextContent( 'A' ) );
+ $this->assertFalse( $record->isInherited() );
+ }
+
+}
--- /dev/null
+<?php
+
+namespace MediaWiki\Tests\Storage;
+
+use Language;
+use MediaWiki\MediaWikiServices;
+use MediaWiki\Storage\SqlBlobStore;
+use MediaWikiTestCase;
+use stdClass;
+use TitleValue;
+
+/**
+ * @covers \MediaWiki\Storage\SqlBlobStore
+ * @group Database
+ */
+class SqlBlobStoreTest extends MediaWikiTestCase {
+
+ /**
+ * @return SqlBlobStore
+ */
+ public function getBlobStore( $legacyEncoding = false, $compressRevisions = false ) {
+ $services = MediaWikiServices::getInstance();
+
+ $store = new SqlBlobStore(
+ $services->getDBLoadBalancer(),
+ $services->getMainWANObjectCache()
+ );
+
+ if ( $compressRevisions ) {
+ $store->setCompressBlobs( $compressRevisions );
+ }
+ if ( $legacyEncoding ) {
+ $store->setLegacyEncoding( $legacyEncoding, Language::factory( 'en' ) );
+ }
+
+ return $store;
+ }
+
+ /**
+ * @covers SqlBlobStore::getCompressBlobs()
+ * @covers SqlBlobStore::setCompressBlobs()
+ */
+ public function testGetSetCompressRevisions() {
+ $store = $this->getBlobStore();
+ $this->assertFalse( $store->getCompressBlobs() );
+ $store->setCompressBlobs( true );
+ $this->assertTrue( $store->getCompressBlobs() );
+ }
+
+ /**
+ * @covers SqlBlobStore::getLegacyEncoding()
+ * @covers SqlBlobStore::getLegacyEncodingConversionLang()
+ * @covers SqlBlobStore::setLegacyEncoding()
+ */
+ public function testGetSetLegacyEncoding() {
+ $store = $this->getBlobStore();
+ $this->assertFalse( $store->getLegacyEncoding() );
+ $this->assertNull( $store->getLegacyEncodingConversionLang() );
+ $en = Language::factory( 'en' );
+ $store->setLegacyEncoding( 'foo', $en );
+ $this->assertSame( 'foo', $store->getLegacyEncoding() );
+ $this->assertSame( $en, $store->getLegacyEncodingConversionLang() );
+ }
+
+ /**
+ * @covers SqlBlobStore::getCacheExpiry()
+ * @covers SqlBlobStore::setCacheExpiry()
+ */
+ public function testGetSetCacheExpiry() {
+ $store = $this->getBlobStore();
+ $this->assertSame( 604800, $store->getCacheExpiry() );
+ $store->setCacheExpiry( 12 );
+ $this->assertSame( 12, $store->getCacheExpiry() );
+ }
+
+ /**
+ * @covers SqlBlobStore::getUseExternalStore()
+ * @covers SqlBlobStore::setUseExternalStore()
+ */
+ public function testGetSetUseExternalStore() {
+ $store = $this->getBlobStore();
+ $this->assertFalse( $store->getUseExternalStore() );
+ $store->setUseExternalStore( true );
+ $this->assertTrue( $store->getUseExternalStore() );
+ }
+
+ public function provideDecompress() {
+ yield '(no legacy encoding), false in false out' => [ false, false, [], false ];
+ yield '(no legacy encoding), empty in empty out' => [ false, '', [], '' ];
+ yield '(no legacy encoding), empty in empty out' => [ false, 'A', [], 'A' ];
+ yield '(no legacy encoding), string in with gzip flag returns string' => [
+ // gzip string below generated with gzdeflate( 'AAAABBAAA' )
+ false, "sttttr\002\022\000", [ 'gzip' ], 'AAAABBAAA',
+ ];
+ yield '(no legacy encoding), string in with object flag returns false' => [
+ // gzip string below generated with serialize( 'JOJO' )
+ false, "s:4:\"JOJO\";", [ 'object' ], false,
+ ];
+ yield '(no legacy encoding), serialized object in with object flag returns string' => [
+ false,
+ // Using a TitleValue object as it has a getText method (which is needed)
+ serialize( new TitleValue( 0, 'HHJJDDFF' ) ),
+ [ 'object' ],
+ 'HHJJDDFF',
+ ];
+ yield '(no legacy encoding), serialized object in with object & gzip flag returns string' => [
+ false,
+ // Using a TitleValue object as it has a getText method (which is needed)
+ gzdeflate( serialize( new TitleValue( 0, '8219JJJ840' ) ) ),
+ [ 'object', 'gzip' ],
+ '8219JJJ840',
+ ];
+ yield '(ISO-8859-1 encoding), string in string out' => [
+ 'ISO-8859-1',
+ iconv( 'utf8', 'ISO-8859-1', "1®Àþ1" ),
+ [],
+ '1®Àþ1',
+ ];
+ yield '(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [
+ 'ISO-8859-1',
+ gzdeflate( iconv( 'utf8', 'ISO-8859-1', "4®Àþ4" ) ),
+ [ 'gzip' ],
+ '4®Àþ4',
+ ];
+ yield '(ISO-8859-1 encoding), serialized object in with object flags returns string' => [
+ 'ISO-8859-1',
+ serialize( new TitleValue( 0, iconv( 'utf8', 'ISO-8859-1', "3®Àþ3" ) ) ),
+ [ 'object' ],
+ '3®Àþ3',
+ ];
+ yield '(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [
+ 'ISO-8859-1',
+ gzdeflate( serialize( new TitleValue( 0, iconv( 'utf8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
+ [ 'gzip', 'object' ],
+ '2®Àþ2',
+ ];
+ }
+
+ /**
+ * @dataProvider provideDecompress
+ * @covers SqlBlobStore::decompressData
+ *
+ * @param string|bool $legacyEncoding
+ * @param mixed $data
+ * @param array $flags
+ * @param mixed $expected
+ */
+ public function testDecompressData( $legacyEncoding, $data, $flags, $expected ) {
+ $store = $this->getBlobStore( $legacyEncoding );
+ $this->assertSame(
+ $expected,
+ $store->decompressData( $data, $flags )
+ );
+ }
+
+ /**
+ * @covers SqlBlobStore::compressData
+ */
+ public function testCompressRevisionTextUtf8() {
+ $store = $this->getBlobStore();
+ $row = new stdClass;
+ $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
+ $row->old_flags = $store->compressData( $row->old_text );
+ $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
+ "Flags should contain 'utf-8'" );
+ $this->assertFalse( false !== strpos( $row->old_flags, 'gzip' ),
+ "Flags should not contain 'gzip'" );
+ $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
+ $row->old_text, "Direct check" );
+ }
+
+ /**
+ * @covers SqlBlobStore::compressData
+ */
+ public function testCompressRevisionTextUtf8Gzip() {
+ $store = $this->getBlobStore( false, true );
+ $this->checkPHPExtension( 'zlib' );
+
+ $row = new stdClass;
+ $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
+ $row->old_flags = $store->compressData( $row->old_text );
+ $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
+ "Flags should contain 'utf-8'" );
+ $this->assertTrue( false !== strpos( $row->old_flags, 'gzip' ),
+ "Flags should contain 'gzip'" );
+ $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
+ gzinflate( $row->old_text ), "Direct check" );
+ }
+
+ public function provideBlobs() {
+ yield [ '' ];
+ yield [ 'someText' ];
+ }
+
+ /**
+ * @dataProvider provideBlobs
+ * @covers SqlBlobStore::storeBlob
+ * @covers SqlBlobStore::getBlob
+ */
+ public function testSimpleStoreGetBlobSimpleRoundtrip( $blob ) {
+ $store = $this->getBlobStore();
+ $address = $store->storeBlob( $blob );
+ $this->assertSame( $blob, $store->getBlob( $address ) );
+ }
+
+}